// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import org.joml.Vector2f;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.Core;

public final class IsoUtils {
    public static float clamp(float x, float minVal, float maxVal) {
        return Math.min(Math.max(x, minVal), maxVal);
    }

    public static float lerp(float val, float min, float max) {
        return max == min ? min : (clamp(val, min, max) - min) / (max - min);
    }

    public static float smoothstep(float edge0, float edge1, float x) {
        float _float = clamp((x - edge0) / (edge1 - edge0), 0.0F, 1.0F);
        return _float * _float * (3.0F - 2.0F * _float);
    }

    public static float DistanceTo(float fromX, float fromY, float toX, float toY) {
        return (float)Math.sqrt(Math.pow((double)(toX - fromX), 2.0) + Math.pow((double)(toY - fromY), 2.0));
    }

    public static float DistanceTo2D(float fromX, float fromY, float toX, float toY) {
        return (float)Math.sqrt(Math.pow((double)(toX - fromX), 2.0) + Math.pow((double)(toY - fromY), 2.0));
    }

    public static float DistanceTo(float fromX, float fromY, float fromZ, float toX, float toY, float toZ) {
        return (float)Math.sqrt(Math.pow((double)(toX - fromX), 2.0) + Math.pow((double)(toY - fromY), 2.0) + Math.pow((double)(toZ - fromZ), 2.0));
    }

    public static float DistanceToSquared(float fromX, float fromY, float fromZ, float toX, float toY, float toZ) {
        return (float)(Math.pow((double)(toX - fromX), 2.0) + Math.pow((double)(toY - fromY), 2.0) + Math.pow((double)(toZ - fromZ), 2.0));
    }

    public static float DistanceToSquared(float fromX, float fromY, float toX, float toY) {
        return (float)(Math.pow((double)(toX - fromX), 2.0) + Math.pow((double)(toY - fromY), 2.0));
    }

    public static float DistanceManhatten(float fromX, float fromY, float toX, float toY) {
        return Math.abs(toX - fromX) + Math.abs(toY - fromY);
    }

    public static float DistanceManhatten(float fromX, float fromY, float toX, float toY, float fromZ, float toZ) {
        return Math.abs(toX - fromX) + Math.abs(toY - fromY) + Math.abs(toZ - fromZ) * 2.0F;
    }

    public static float DistanceManhattenSquare(float fromX, float fromY, float toX, float toY) {
        return Math.max(Math.abs(toX - fromX), Math.abs(toY - fromY));
    }

    public static float XToIso(float screenX, float screenY, float floor) {
        float float0 = screenX + IsoCamera.getOffX();
        float float1 = screenY + IsoCamera.getOffY();
        float float2 = (float0 + 2.0F * float1) / (64.0F * (float)Core.TileScale);
        float float3 = (float0 - 2.0F * float1) / (-64.0F * (float)Core.TileScale);
        float2 += 3.0F * floor;
        float3 += 3.0F * floor;
        return float2;
    }

    public static float XToIsoTrue(float screenX, float screenY, int floor) {
        float float0 = screenX + (float)((int)IsoCamera.cameras[IsoPlayer.getPlayerIndex()].OffX);
        float float1 = screenY + (float)((int)IsoCamera.cameras[IsoPlayer.getPlayerIndex()].OffY);
        float float2 = (float0 + 2.0F * float1) / (64.0F * (float)Core.TileScale);
        float float3 = (float0 - 2.0F * float1) / (-64.0F * (float)Core.TileScale);
        float2 += (float)(3 * floor);
        float3 += (float)(3 * floor);
        return float2;
    }

    public static float XToScreen(float objectX, float objectY, float objectZ, int screenZ) {
        float _float = 0.0F;
        _float += objectX * (float)(32 * Core.TileScale);
        return _float - objectY * (float)(32 * Core.TileScale);
    }

    public static float XToScreenInt(int objectX, int objectY, int objectZ, int screenZ) {
        return XToScreen((float)objectX, (float)objectY, (float)objectZ, screenZ);
    }

    public static float YToScreenExact(float objectX, float objectY, float objectZ, int screenZ) {
        float _float = YToScreen(objectX, objectY, objectZ, screenZ);
        return _float - IsoCamera.getOffY();
    }

    public static float XToScreenExact(float objectX, float objectY, float objectZ, int screenZ) {
        float _float = XToScreen(objectX, objectY, objectZ, screenZ);
        return _float - IsoCamera.getOffX();
    }

    public static float YToIso(float screenX, float screenY, float floor) {
        float float0 = screenX + IsoCamera.getOffX();
        float float1 = screenY + IsoCamera.getOffY();
        float float2 = (float0 + 2.0F * float1) / (64.0F * (float)Core.TileScale);
        float float3 = (float0 - 2.0F * float1) / (-64.0F * (float)Core.TileScale);
        float2 += 3.0F * floor;
        return float3 + 3.0F * floor;
    }

    public static float YToScreen(float objectX, float objectY, float objectZ, int screenZ) {
        float _float = 0.0F;
        _float += objectY * (float)(16 * Core.TileScale);
        _float += objectX * (float)(16 * Core.TileScale);
        return _float + ((float)screenZ - objectZ) * (float)(96 * Core.TileScale);
    }

    public static float YToScreenInt(int objectX, int objectY, int objectZ, int screenZ) {
        return YToScreen((float)objectX, (float)objectY, (float)objectZ, screenZ);
    }

    public static boolean isSimilarDirection(IsoGameCharacter chr, float xA, float yA, float xB, float yB, float similar) {
        Vector2f vector2f0 = new Vector2f(xA - chr.x, yA - chr.y);
        vector2f0.normalize();
        Vector2f vector2f1 = new Vector2f(chr.x - xB, chr.y - yB);
        vector2f1.normalize();
        vector2f0.add(vector2f1);
        return vector2f0.length() < similar;
    }
}
